import DefaultTable from "@/components/DefaultTable";
import { getBreadcRumb, push } from "@/utils/util";
import { Space, Button, message, Popconfirm } from "antd";
import { ColumnsType } from 'antd/es/table'
import { useEffect, useRef, useState } from "react";
import { deleteBoss, updatePrice } from '@/apis/boss/index'
import DefaultModal from "@/components/DefaultModal";
import { save } from "@/apis/order";
import SearchForm from "@/components/SearchForm";
function BossManage() {
    getBreadcRumb([{ name: "老板列表" }])
    const [updatePriceVisible, setupdatePriceVisible] = useState(false)//存单弹框
    const [bossId, setbossId] = useState('')
    const TableRef = useRef<TableRefParams>(null)
    const [orderListVisible, setorderListVisible] = useState(false)//下单列表
    const [values, setvalues] = useState({})
    const updatePriceList: listItem[] = [
        {
            label: "存入金额",
            name: "amount",
            type: "input",
            rules: [{ required: true, message: "请输入存入金额" }],
        }
    ]
    const columns: ColumnsType<BaselistItem> = [
        {
            title: "序号",
            dataIndex: "itemSn",
            render: (_text, record, index) => (((record.pageNum || 0) - 1) * (record.pageSize || 0) + index + 1)
        },
        {
            title: "老板编号",
            dataIndex: "bossId",
        },
        {
            title: "老板名称",
            dataIndex: "bossName",
        },
        {
            title: "当前剩余金额",
            dataIndex: "amount",
        },
        {
            title: "操作",
            dataIndex: "index",
            render: (_index: any, data: any) => {
                return (
                    <Space>
                        <a onClick={() => push('/bossList/look/' + data.bossId)}>查看</a>
                        <a onClick={() => push('/bossList/edit/' + data.bossId)}>编辑</a>
                        <a onClick={() => onSavePriceBtn(data.bossId)}>存入金额</a>
                        <a onClick={() => oncheckoutBtn(data.bossId)}>下单</a>
                        <Popconfirm title='是否要删除' onConfirm={() => ondelBtn(data.bossId)}>
                            <a >删除</a>
                        </Popconfirm>

                    </Space>
                );
            },
        },
    ]
    const orderList: listItem[] = [
        {
            label: "陪玩名称",
            name: "playWiths",
            type: "playWithSelect",
            rules: [{ required: true, message: '请选择陪玩名称' }]
        },
        {
            label: "下单时长（h）",
            name: "orderTime",
            type: "input",
            rules: [{ required: true, message: '请输入下单时长' }]
        },
    ]
    const searchList: listItem[] = [
        {
            label: "老板名称",
            name: "bossName",
            type: "input"
        },
    ]
    //老板下单
    async function oncheckoutBtn(bossId: string) {
        setbossId(bossId)
        setorderListVisible(true)
    }
    //存入金额
    function onSavePriceBtn(bossId: string) {
        setbossId(bossId)
        setupdatePriceVisible(true)
    }
    //删除
    async function ondelBtn(bossId: string) {
        await deleteBoss({ bossId })
        message.success('删除成功')
        TableRef.current?.getList()
    }
    function getCreateBtn() {
        push('/bossList/create');
    }
    useEffect(() => {
        TableRef.current?.getList()
    }, [])
    async function onUpdateOkBtn(value: { amount: string }) {
        await updatePrice({ bossId: bossId, amount: parseInt(value.amount) })
        message.success('存入成功')
        setupdatePriceVisible(false)
        TableRef.current?.getList()
    }
    //下单按钮
    async function handleOrderListBtn(values: any) {
        console.log(values)
        values.bossId = bossId
        values.orderTime = parseInt(values.orderTime)
        await save(values)
        message.success('下单成功')
        setorderListVisible(false)
        TableRef.current?.getList()
    }
    function onFinish(values: any) {
        setvalues(values)
    }
    return (
        <div>
            <div className='tableWrap'>
                <SearchForm onFinish={onFinish} list={searchList}></SearchForm>
                <Space style={{ marginBottom: '8px' }}>
                    <Button type="primary" onClick={getCreateBtn}>添加</Button>
                </Space>
                <DefaultTable columns={columns} values={values} ref={TableRef} api={'/boss/list'} />
            </div>
            <DefaultModal open={updatePriceVisible} list={updatePriceList} title='存入金额' onCancel={() => setupdatePriceVisible(false)} onOk={onUpdateOkBtn} ></DefaultModal>
            <DefaultModal open={orderListVisible} list={orderList} title='订单列表' onCancel={() => setorderListVisible(false)} onOk={handleOrderListBtn} ></DefaultModal>
        </div>
    )
}



export default BossManage;
